import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home' // 重定向
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
      meta: {
        isshow: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/fenlei',
      name: 'fenlei',
      component: () => import('@/views/FenleiView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/xq',
      name: 'xiangqing',
      component: () => import('@/views/xiangqing.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@/views/CartView.vue'),
      meta: {
        isshow: true
      }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@/views/UserView.vue'),
      meta: {
        isshow: true
      }
    },
    {
      path: '/userdata',
      name: 'userdata',
      component: () => import('@/views/UserdataView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/userdd',
      name: 'userdd',
      component: () => import('@/views/Userdd.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/AdressList',
      name: 'AdressList',
      component: () => import('@/views/AdressList.vue'),
      meta: {
        isshow: false
      }
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: ()=>import('../views/NotFoundView.vue') },
  ]
})
router.beforeEach((to, from) => {
  if (to.path == '/cart') {
      if (!localStorage.getItem('user_code')) {
          // 表示本地没有token,获取的值为null
          // ElMessage({
          //     message: '用户未登录,请先登录!!!',
          //     type: 'warning',
          //     duration: 2000,
          // })
              alert('用户未登录,请先登录!')
          return { path: '/login' }
      }
  } 
  else if(to.path == '/login') {
      // 进入登录页
      if (localStorage.getItem('user_code')) {
          // 判断有没有token,有的话,友好提示
          // ElMessage({
          //     message: '登录状态有效,不需要重复登录!!!',
          //     type: 'warning',
          //     duration: 2000,
          // })
              alert('登录状态有效,不需要重复登录')

          return {
              path: '/home'
          }
      }

  }

})

// function ElMessage(arg0: { message: string; type: string; duration: number }) {
//   throw new Error('Function not implemented.')
// }
export default router



